from copy import copy
class MAsort:
    def __init__(self,mode=None):
        self.mode=mode
        if mode in ["long","buy"]:
            self.oncalc = self.oncalc_long
        elif mode in ["short","sell"]:
            self.oncalc = self.oncalc_short
        else:
            self.oncalc=self.oncalc0
    def oncalc0(self,*b,timekey=None):
        if None not in b:
            b=list(b)
            a=copy(b)
            b.sort()
            if a==b:
                return "sell"
            b.sort(reverse=True)
            if a==b:
                return "buy"
            else:
                return None
        else:
            return None
    def oncalc_short(self,*b,timekey=None):
        if None not in b:
            b=list(b)
            a=copy(b)
            b.sort()
            if a==b:
                return "sell"
            else:
                return None
        else:
            return None
    def oncalc_long(self,*b,timekey=None):
        if None not in b:
            b=list(b)
            a=copy(b)
            b.sort(reverse=True)
            if a==b:
                return "buy"
            else:
                return None
        else:
            return None
if __name__ == '__main__':
    from jili.core import load, save
    from research.calcor.calcors_graph import graph_calcor
    import pandas as pd

    MAsort0=MAsort(mode="sell")
    print(MAsort0.oncalc(10,20,30,40))
    print(MAsort0.oncalc(10, 30, 30, 40))
    print(MAsort0.oncalc(10, 40, 30, 40))
    print(MAsort0.oncalc(40, 30, 20, 10))
    # k1m = load(r"D:\data\future_k1m_tq\TA005\TA005_20200102.pkl")
    # cc = [
    #     {'calc_cmd': 'ta', 'cmd': 'MA', 'out': ['SMA5'], 'input': {'price': 'close'}, 'arg': {'timeperiod': 5, 'matype': 0}, 'batch': 5},
    #     {'calc_cmd': 'ta', 'cmd': 'MA', 'out': ['SMA10'], 'input': {'price': 'close'},
    #      'arg': {'timeperiod': 10, 'matype': 0}, 'batch': 10},
    #     {'calc_cmd': 'ta', 'cmd': 'MA', 'out': ['SMA20'], 'input': {'price': 'close'},
    #      'arg': {'timeperiod': 20, 'matype': 0}, 'batch': 20},
    #     {'calc_cmd': 'ta', 'cmd': 'MA', 'out': ['SMA40'], 'input': {'price': 'close'},
    #      'arg': {'timeperiod': 40, 'matype': 0}, 'batch': 40},
    #     {'calc_cmd': 'ta', 'cmd': 'MA', 'out': ['SMA60'], 'input': {'price': 'close'},
    #      'arg': {'timeperiod': 60, 'matype': 0}, 'batch': 60},
    #     {"calc_cmd":"class","class":"MAsort","input":["SMA5","SMA10","SMA20","SMA40","SMA60"],"out":["MAsort"]}
    # ]
    # c = graph_calcor(cc)
    # for v in k1m:
    #     v= c.onbar(v)
    # k11md=pd.DataFrame(k1m)
    # print("end")